package com.wzy.Controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wzy.entity.Blog;

import com.wzy.entity.Video;
import com.wzy.service.BlogService;
import com.wzy.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器，首页
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Api("首页控制器")
@Slf4j
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private VideoService videoService;


    /**
     * 罗列出年份对应的事件,如果没有传递过来时间轴的信息，那就按照默认年份(1931年开始)显示数据
     * 默认1页，每页6条数据
     *
     * @return
     */
    @ApiOperation("首页数据渲染")
    @GetMapping({"/index/{current}/{limit}"})
    public String routerIndexPage(
            @PathVariable() Long current,
            @PathVariable() Long limit,
            @RequestParam(required = false, value = "date")
                    String date,
            Model model) {
//        分页查询
        Page<Blog> blogPage = new Page<>(current, limit);
//       得到数据
        blogService.getBlog(blogPage, date);
        List<Blog> records = blogPage.getRecords();
        //分页的数据总数
        Long total = blogPage.getTotal();
        //推荐视频查询,显示4个够劲了
        List<Video> videos = videoService.getVideo(4);
        System.out.println(videos.toString());
        model.addAttribute("videos", videos);
        model.addAttribute("blogRecords", records);
        model.addAttribute("total", total);
        model.addAttribute("hasNext", blogPage.hasNext());
        model.addAttribute("hasPrevious", blogPage.hasPrevious());
        return "index";
    }


}

